{
 "cells": [
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "# 根据正常和故障时间区间给原始数据打上标签\n",
    "-1：无效数据，即不再给定的时间区间内\n",
    "\n",
    "0：正常数据\n",
    "\n",
    "1：故障数据，即结冰数据"
   ],
   "id": "1325af209a6414c2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:48:53.558752Z",
     "start_time": "2024-10-23T02:48:53.553748Z"
    }
   },
   "cell_type": "code",
   "source": [
    "import pandas as pd\n",
    "from datetime import datetime"
   ],
   "id": "initial_id",
   "outputs": [],
   "execution_count": 27
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": [
    "data数据格式\n",
    "\n",
    "| time                | wind_speed       | generator_speed  | power            | wind_direction    | wind_direction_mean | yaw_position       | yaw_speed         | pitch1_angle      | pitch2_angle      | pitch3_angle      | pitch1_speed | pitch2_speed | pitch3_speed | pitch1_moto_tmp | pitch2_moto_tmp | pitch3_moto_tmp | acc_x             | acc_y              | environment_tmp    | int_tmp            | pitch1_ng5_tmp   | pitch2_ng5_tmp   | pitch3_ng5_tmp    | pitch1_ng5_DC | pitch2_ng5_DC | pitch3_ng5_DC | group |\n",
    "| ------------------- | ---------------- | ---------------- | ---------------- | ----------------- | ------------------- | ------------------ | ----------------- | ----------------- | ----------------- | ----------------- | ------------ | ------------ | ------------ | --------------- | --------------- | --------------- | ----------------- | ------------------ | ------------------ | ------------------ | ---------------- | ---------------- | ----------------- | ------------- | ------------- | ------------- | ----- |\n",
    "| 2015-11-01 20:20:16 | 1.85999330468501 | 1.22359452534749 | 2.51578969727773 | -2.07273948754782 | -2.07362658911582   | -0.655342751976593 | 0.030803582422607 | 0.555555555555556 | 0.506666666666667 | 0.551111111111111 | -1.68        | -1.72        | -1.68        | 0.759           | 0.6             | 0.59            | -1.02398638496058 | 0.0611094692486426 | -0.403918740333512 | 0.0149184125297424 | 1.30769230769231 | 1.12307692307692 | 0.783076923076923 | 1.36          | 0             | 1.56          | 1     |\n",
    "\n",
    "info数据格式\n",
    "\n",
    "| startTime           | endTime                  |\n",
    "|---------------------|--------------------------|\n",
    "| 2015-11-01 20:20:16 | 2015-11-03 23:47:32      |\n"
   ],
   "id": "7a163cf15721c0bb"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:48:55.466204Z",
     "start_time": "2024-10-23T02:48:55.460760Z"
    }
   },
   "cell_type": "code",
   "source": [
    "data_num = 15\n",
    "dir = f'../data/raw/train'\n",
    "data_file = f'{dir}/{data_num}/{data_num}_data.csv'\n",
    "data_normal_file = f'{dir}/{data_num}/{data_num}_normalInfo.csv'\n",
    "data_failure_file = f'{dir}/{data_num}/{data_num}_failureInfo.csv'\n",
    "save_file = f'./data/{data_num}_label.csv'"
   ],
   "id": "a6151d4ee6d750b0",
   "outputs": [],
   "execution_count": 28
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:48:57.242386Z",
     "start_time": "2024-10-23T02:48:56.578184Z"
    }
   },
   "cell_type": "code",
   "source": "df = pd.read_csv(data_file)",
   "id": "d28bd77a661638e2",
   "outputs": [],
   "execution_count": 29
  },
  {
   "metadata": {},
   "cell_type": "markdown",
   "source": "# 打标签",
   "id": "d6ea252b1af226d2"
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:48:59.946055Z",
     "start_time": "2024-10-23T02:48:59.901120Z"
    }
   },
   "cell_type": "code",
   "source": [
    "df[\"label\"] = -1\n",
    "print(df)"
   ],
   "id": "5a6a6f8e67906eb0",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "                       time  wind_speed  generator_speed     power  \\\n",
      "0       2015-11-01 20:20:16    1.859993         1.223595  2.515790   \n",
      "1       2015-11-01 20:20:23    1.911625         1.293394  2.313551   \n",
      "2       2015-11-01 20:20:30    1.635027         1.280099  2.507799   \n",
      "3       2015-11-01 20:20:37    1.786234         1.280099  2.349593   \n",
      "4       2015-11-01 20:20:47    1.786234         1.263480  2.321566   \n",
      "...                     ...         ...              ...       ...   \n",
      "393881  2016-01-01 21:37:32   -0.113069        -1.179516 -0.883126   \n",
      "393882  2016-01-01 21:37:39    0.034450        -1.136307 -0.871127   \n",
      "393883  2016-01-01 21:37:46    0.292608        -1.129659 -0.877126   \n",
      "393884  2016-01-01 21:37:53   -0.035621        -1.159574 -0.877126   \n",
      "393885  2016-01-01 21:38:01    0.226224        -1.166221 -0.863113   \n",
      "\n",
      "        wind_direction  wind_direction_mean  yaw_position  yaw_speed  \\\n",
      "0            -2.072739            -2.073627     -0.655343   0.030804   \n",
      "1            -2.010591            -1.615140     -0.655343   0.030804   \n",
      "2            -2.053750            -0.282742     -0.649566   0.170338   \n",
      "3            -2.007138            -2.234477     -0.655343  -0.004080   \n",
      "4            -2.264365            -1.428959     -0.637917   0.414524   \n",
      "...                ...                  ...           ...        ...   \n",
      "393881        0.033416             0.826745     -0.742660  -0.527336   \n",
      "393882        0.145629             0.330262     -0.742660  -0.457568   \n",
      "393883       -0.085703            -1.540414     -0.731058  -0.073848   \n",
      "393884       -0.622600            -1.442891     -0.731058  -0.073848   \n",
      "393885       -0.803867            -0.487921     -0.725234   0.100571   \n",
      "\n",
      "        pitch1_angle  pitch2_angle  ...  environment_tmp   int_tmp  \\\n",
      "0           0.555556      0.506667  ...        -0.403919  0.014918   \n",
      "1           0.195556      0.133333  ...        -0.421277 -0.002291   \n",
      "2           0.964444      0.951111  ...        -0.421277 -0.002291   \n",
      "3           0.168889      0.137778  ...        -0.403919 -0.002291   \n",
      "4           0.182222      0.168889  ...        -0.403919  0.014918   \n",
      "...              ...           ...  ...              ...       ...   \n",
      "393881      0.204444      0.195556  ...        -1.620604 -1.665902   \n",
      "393882      0.204444      0.195556  ...        -1.620604 -1.665902   \n",
      "393883      0.204444      0.195556  ...        -1.620604 -1.686936   \n",
      "393884      0.204444      0.195556  ...        -1.620604 -1.665902   \n",
      "393885      0.204444      0.195556  ...        -1.636385 -1.686936   \n",
      "\n",
      "        pitch1_ng5_tmp  pitch2_ng5_tmp  pitch3_ng5_tmp  pitch1_ng5_DC  \\\n",
      "0             1.307692        1.123077        0.783077           1.36   \n",
      "1             1.307692        1.123077        0.783077           0.44   \n",
      "2             1.307692        1.123077        0.783077           1.76   \n",
      "3             1.307692        1.123077        0.783077           2.80   \n",
      "4             1.307692        1.123077        0.783077          -0.88   \n",
      "...                ...             ...             ...            ...   \n",
      "393881        1.000000        1.013846        1.000000          -0.52   \n",
      "393882        1.000000        1.013846        1.000000           0.32   \n",
      "393883        1.000000        1.013846        1.000000           1.20   \n",
      "393884        1.013846        1.029231        1.000000           0.76   \n",
      "393885        1.013846        1.046154        1.000000          -0.36   \n",
      "\n",
      "        pitch2_ng5_DC  pitch3_ng5_DC  group  label  \n",
      "0                0.00           1.56      1     -1  \n",
      "1                2.88          -2.60      1     -1  \n",
      "2                0.60           2.56      1     -1  \n",
      "3               -0.48           0.12      1     -1  \n",
      "4                1.72           0.92      1     -1  \n",
      "...               ...            ...    ...    ...  \n",
      "393881          -0.04          -1.48   3853     -1  \n",
      "393882          -2.36           1.84   3853     -1  \n",
      "393883           0.12           1.28   3853     -1  \n",
      "393884          -0.40          -0.60   3853     -1  \n",
      "393885           1.52           0.60   3853     -1  \n",
      "\n",
      "[393886 rows x 29 columns]\n"
     ]
    }
   ],
   "execution_count": 30
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:49:02.995192Z",
     "start_time": "2024-10-23T02:49:02.981066Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# \"startTime\",\"endTime\"\n",
    "# 2015-11-01 20:20:16,2015-11-03 23:47:32\n",
    "df_normal = pd.read_csv(data_normal_file)\n",
    "df_normal[\"startTime\"] = pd.to_datetime(df_normal[\"startTime\"])\n",
    "df_normal[\"endTime\"] = pd.to_datetime(df_normal[\"endTime\"])\n",
    "normal_time_list = [(row[\"startTime\"], row[\"endTime\"]) for _, row in df_normal.iterrows()]\n",
    "\n",
    "df_failure = pd.read_csv(data_failure_file)\n",
    "df_failure[\"startTime\"] = pd.to_datetime(df_failure[\"startTime\"])\n",
    "df_failure[\"endTime\"] = pd.to_datetime(df_failure[\"endTime\"])\n",
    "failure_time_list = [(row[\"startTime\"], row[\"endTime\"]) for  _, row in df_failure.iterrows()]"
   ],
   "id": "396842935be65eda",
   "outputs": [],
   "execution_count": 31
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-23T02:49:04.266817Z",
     "start_time": "2024-10-23T02:49:04.254813Z"
    }
   },
   "cell_type": "code",
   "source": [
    "print(normal_time_list)\n",
    "print(failure_time_list)"
   ],
   "id": "a027abc3019d64d3",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[(Timestamp('2015-11-01 20:20:16'), Timestamp('2015-11-03 23:47:32')), (Timestamp('2015-11-04 14:04:51'), Timestamp('2015-11-04 18:26:08')), (Timestamp('2015-11-05 11:06:59'), Timestamp('2015-11-09 02:44:31')), (Timestamp('2015-11-09 12:30:09'), Timestamp('2015-11-09 19:23:32')), (Timestamp('2015-11-10 00:04:50'), Timestamp('2015-11-15 23:03:06')), (Timestamp('2015-11-17 11:37:50'), Timestamp('2015-11-20 22:35:45')), (Timestamp('2015-11-21 11:10:51'), Timestamp('2015-11-22 22:50:24')), (Timestamp('2015-11-23 11:50:58'), Timestamp('2015-11-23 16:47:57')), (Timestamp('2015-11-26 08:46:48'), Timestamp('2015-11-29 00:05:50')), (Timestamp('2015-11-29 11:48:50'), Timestamp('2015-11-29 14:22:15')), (Timestamp('2015-11-30 10:11:06'), Timestamp('2015-11-30 16:50:17')), (Timestamp('2015-12-01 13:30:37'), Timestamp('2015-12-03 15:14:13')), (Timestamp('2015-12-03 21:22:30'), Timestamp('2015-12-04 18:43:01')), (Timestamp('2015-12-05 14:13:10'), Timestamp('2015-12-05 15:32:16')), (Timestamp('2015-12-06 13:00:58'), Timestamp('2015-12-07 14:26:30')), (Timestamp('2015-12-09 08:00:09'), Timestamp('2015-12-09 13:45:04')), (Timestamp('2015-12-10 07:22:36'), Timestamp('2015-12-11 22:40:24')), (Timestamp('2015-12-12 12:06:00'), Timestamp('2015-12-12 13:11:04')), (Timestamp('2015-12-12 22:01:28'), Timestamp('2015-12-12 22:16:45')), (Timestamp('2015-12-13 11:05:39'), Timestamp('2015-12-13 20:27:05')), (Timestamp('2015-12-14 12:29:58'), Timestamp('2015-12-15 19:47:34')), (Timestamp('2015-12-16 09:08:49'), Timestamp('2015-12-18 06:06:42')), (Timestamp('2015-12-18 11:49:26'), Timestamp('2015-12-19 16:56:38')), (Timestamp('2015-12-20 13:59:41'), Timestamp('2015-12-22 06:57:43')), (Timestamp('2015-12-22 23:23:35'), Timestamp('2015-12-26 19:58:42')), (Timestamp('2015-12-27 11:56:09'), Timestamp('2016-01-01 17:51:07'))]\n",
      "[(Timestamp('2015-11-04 21:37:06'), Timestamp('2015-11-04 22:29:33')), (Timestamp('2015-11-09 04:43:09'), Timestamp('2015-11-09 06:35:39')), (Timestamp('2015-11-09 21:21:52'), Timestamp('2015-11-09 23:14:41')), (Timestamp('2015-11-16 03:51:54'), Timestamp('2015-11-16 11:06:26')), (Timestamp('2015-11-16 14:15:53'), Timestamp('2015-11-16 16:08:25')), (Timestamp('2015-11-21 10:07:48'), Timestamp('2015-11-21 10:20:07')), (Timestamp('2015-11-23 06:58:07'), Timestamp('2015-11-23 07:39:43')), (Timestamp('2015-11-23 18:46:31'), Timestamp('2015-11-23 20:39:27')), (Timestamp('2015-11-24 07:16:23'), Timestamp('2015-11-24 09:09:18')), (Timestamp('2015-11-24 17:26:57'), Timestamp('2015-11-26 01:40:43')), (Timestamp('2015-11-29 02:04:41'), Timestamp('2015-11-29 03:57:16')), (Timestamp('2015-11-29 17:30:39'), Timestamp('2015-11-30 06:50:45')), (Timestamp('2015-11-30 23:22:08'), Timestamp('2015-11-30 23:39:17')), (Timestamp('2015-12-03 17:10:36'), Timestamp('2015-12-03 19:01:06')), (Timestamp('2015-12-04 21:47:55'), Timestamp('2015-12-05 01:00:10')), (Timestamp('2015-12-05 17:28:39'), Timestamp('2015-12-05 19:19:12')), (Timestamp('2015-12-07 16:22:53'), Timestamp('2015-12-07 18:13:19')), (Timestamp('2015-12-08 19:27:03'), Timestamp('2015-12-08 20:59:41')), (Timestamp('2015-12-09 15:41:26'), Timestamp('2015-12-09 17:31:56')), (Timestamp('2015-12-12 00:36:50'), Timestamp('2015-12-12 02:27:19')), (Timestamp('2015-12-12 15:07:33'), Timestamp('2015-12-12 18:38:02')), (Timestamp('2015-12-13 00:18:00'), Timestamp('2015-12-13 02:03:34')), (Timestamp('2015-12-14 03:33:32'), Timestamp('2015-12-14 07:43:55')), (Timestamp('2015-12-15 21:43:53'), Timestamp('2015-12-15 23:34:24')), (Timestamp('2015-12-18 08:03:05'), Timestamp('2015-12-18 09:11:48')), (Timestamp('2015-12-20 09:53:01'), Timestamp('2015-12-20 11:50:59')), (Timestamp('2015-12-22 20:07:25'), Timestamp('2015-12-22 21:57:52')), (Timestamp('2015-12-26 23:56:28'), Timestamp('2015-12-27 01:46:59')), (Timestamp('2016-01-01 19:47:33'), Timestamp('2016-01-01 21:38:01'))]\n"
     ]
    }
   ],
   "execution_count": 32
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:33.565670Z",
     "start_time": "2024-10-22T11:33:33.551546Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def in_time_list(time, time_list):\n",
    "    return any(start <= time <= end for start, end in time_list)"
   ],
   "id": "a1b2fd0a03b54f81",
   "outputs": [],
   "execution_count": 20
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:33.581784Z",
     "start_time": "2024-10-22T11:33:33.566671Z"
    }
   },
   "cell_type": "code",
   "source": "in_time_list(datetime(2015, 11, 29, 12, 46, 48), normal_time_list)",
   "id": "b5b07293bf56ce7d",
   "outputs": [
    {
     "data": {
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "execution_count": 21
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:33.723996Z",
     "start_time": "2024-10-22T11:33:33.582785Z"
    }
   },
   "cell_type": "code",
   "source": "df['time'] = pd.to_datetime(df['time'])",
   "id": "ead6dfdde5ec043b",
   "outputs": [],
   "execution_count": 22
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:33.738410Z",
     "start_time": "2024-10-22T11:33:33.723996Z"
    }
   },
   "cell_type": "code",
   "source": [
    "def set_label(time):\n",
    "    if in_time_list(time, normal_time_list):\n",
    "        return 0\n",
    "    if in_time_list(time, failure_time_list):\n",
    "        return 1\n",
    "    return -1"
   ],
   "id": "27dd7e2a94125129",
   "outputs": [],
   "execution_count": 23
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:34.850381Z",
     "start_time": "2024-10-22T11:33:33.739409Z"
    }
   },
   "cell_type": "code",
   "source": "df['label'] = df['time'].apply(set_label)",
   "id": "ec84431d7fb25e69",
   "outputs": [],
   "execution_count": 24
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:34.865286Z",
     "start_time": "2024-10-22T11:33:34.851343Z"
    }
   },
   "cell_type": "code",
   "source": "print(f'{data_num}号风机，总数据量：{len(df)}, 正常数据量：{(df[\"label\"] == 0).sum()}，故障数据量：{(df[\"label\"] == 1).sum()}，无效数据量：{(df[\"label\"] == -1).sum()}')",
   "id": "b203934abe1f533d",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "15号风机，总数据量：393886, 正常数据量：350255，故障数据量：23892，无效数据量：19739\n"
     ]
    }
   ],
   "execution_count": 25
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2024-10-22T11:33:39.321674Z",
     "start_time": "2024-10-22T11:33:34.867286Z"
    }
   },
   "cell_type": "code",
   "source": [
    "save = True\n",
    "if save:\n",
    "    df.to_csv(save_file, index=False)"
   ],
   "id": "a35fdb006d9748d6",
   "outputs": [],
   "execution_count": 26
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
